#!/usr/bin/env bash

source "$rvm_scripts_path/functions/alias"

__rvm_check_default()
{
  \typeset default_ruby_interpreter current_ruby_interpreter
  default_ruby_interpreter="$( alias_show default 2>/dev/null )"
  default_ruby_interpreter="${default_ruby_interpreter%%${rvm_gemset_separator:-"@"}*}"
  current_ruby_interpreter="${rvm_ruby_string%%${rvm_gemset_separator:-"@"}*}"
  if
    [[ -n "$current_ruby_interpreter" ]] &&
    [[ "$current_ruby_interpreter" == "$default_ruby_interpreter" ]]
  then
    __rvm_log_command 'default.restore' \
      "$rvm_ruby_string - #removing default ruby interpreter" \
      alias_delete default
  fi
}

__rvm_remove_ruby()
{
  case "${rvm_ruby_string:-}" in
    (*@*)
      rvm_error "'rvm remove' does not work with gemsets, use '--gems' to remove all gemsets or
'rvm ${rvm_ruby_string%@*} do rvm gemset delete ${rvm_ruby_string#*@}' to remove the '${rvm_ruby_string#*@}' gemset."
      return 1
      ;;
  esac
  rvm_fuzzy_flag=1
  (( ${rvm_ruby_selected_flag:=0} )) || __rvm_select
  [[ -n "${rvm_ruby_string:-}"    ]] ||
  {
    rvm_error "Cannot ${1:-remove} unknown package '$rvm_ruby_string'"
    return 1
  }

  \typeset __removed_counter=0
  __rvm_remove_install_record "$rvm_ruby_string"
  __rvm_remove_src_and_ruby      &&
  case "${1:-remove}" in
    (remove)
      __rvm_remove_gemsets       &&
      __rvm_remove_archives      &&
      __rvm_remove_aliases       &&
      __rvm_remove_wrappers      &&
      __rvm_remove_environments  &&
      __rvm_remove_binaries      &&
      __rvm_remove_ruby_done
      ;;
    (uninstall)
      __rvm_remove_gemsets_check &&
      __rvm_check_default        &&
      __rvm_remove_ruby_done
      ;;
    (reinstall)
      __rvm_remove_gemsets_check
      ;;
  esac
  unset rvm_remove_flag
}

__rvm_remove_ruby_done()
{
  (( __removed_counter )) || rvm_log "$rvm_ruby_string - #already gone"
}

__rvm_remove_src_and_ruby()
{
  \typeset _dir
  for _dir in "${rvm_src_path}" "${rvm_rubies_path}"
  do
    if
      [[ -d "${_dir}/$rvm_ruby_string" ]]
    then
      let __removed_counter+=1
      __rvm_log_command "remove.${_dir##*/}" \
        "$rvm_ruby_string - #removing ${_dir##*/}/$rvm_ruby_string" \
        __rvm_rm_rf "${_dir}/$rvm_ruby_string" ||
        return $?
    else
      rvm_debug "$rvm_ruby_string - #already removed ${_dir##*/}/$rvm_ruby_string"
    fi
  done

  if [[ -e "${rvm_bin_path}/$rvm_ruby_string" ]]
  then \command \rm -f "${rvm_bin_path}/$rvm_ruby_string"
  fi
}

__rvm_remove_gemsets_check()
{
  if (( ${rvm_gems_flag:=0} == 1 ))
  then __rvm_remove_gemsets || return $?
  fi
  true
}

__rvm_remove_path_list_remove()
{
  \typeset __item
  for __item
  do
    printf "Removing ${__item} - "
    __rvm_rm_rf "${__item}" && printf "ok\n" ||
    {
      \typeset __res=$?
      echo "failed\n"
      return ${__res}
    }
  done
}

__rvm_remove_path_list_filter()
{
  __list=()
  \typeset __item
  for __item
  do
    if [[ -e "${__item}" || -L "${__item}" ]]
    then __list+=( "${__item}" )
    fi
  done
}

__rvm_remove_path_list()
{
  \typeset -a __list
  \typeset __type_name="$1"
  shift
  __rvm_remove_path_list_filter "$@"
  if
    (( ${#__list[@]} ))
  then
    let __removed_counter+=1
    __rvm_log_command "remove_${__type_name}" \
      "$rvm_ruby_string - #removing ${__type_name}" \
      __rvm_remove_path_list_remove "${__list[@]}" ||
      return $?
  else
    rvm_debug "$rvm_ruby_string - #already removed ${__type_name}"
  fi
  true
}

__rvm_remove_gemsets()
{
  __rvm_remove_path_list gems "${rvm_gems_path:-"$rvm_path/gems"}/${rvm_ruby_string}"{,@*}
}

__rvm_remove_wrappers()
{
  __rvm_remove_path_list wrappers "$rvm_wrappers_path/$rvm_ruby_string"{,@*} "$rvm_bin_path"/*-"$rvm_ruby_string"
}

__rvm_remove_environments()
{
  __rvm_remove_path_list environments "$rvm_environments_path/$rvm_ruby_string"{,@*}
}

__rvm_remove_aliases()
{
  \typeset alias_name
  \typeset -a aliases

  __rvm_read_lines aliases <(
    __rvm_awk '/'$rvm_ruby_string'/{print}' "$rvm_path/config/alias" | __rvm_sed 's/=.*//'
  )
  if
    (( ${#aliases[@]} ))
  then
    let __removed_counter+=1
    rvm_log "$rvm_ruby_string - #removing aliases"
    for alias_name in "${aliases[@]}"
    do
      # Remove from alias key-value store
      "$rvm_scripts_path/alias" delete "$alias_name" >/dev/null 2>&1
    done
  else
    rvm_debug "$rvm_ruby_string - #already removed aliases"
  fi
}

__rvm_remove_archives()
{
  if (( ${rvm_archive_flag:=0} == 1 ))
  then __rvm_remove_path_list archives "${rvm_archives_path}/${rvm_ruby_package_file:-$rvm_ruby_string}".*
  fi
}

# Iterate over all binaries and check for symlinked wrappers etc.
__rvm_remove_binaries()
{
  __rvm_remove_path_list binaries "${rvm_bin_path:-$rvm_path/bin}"/*"$rvm_ruby_string"{,@*}
}
